<script>
  import store from '@/store'
  import { getSceneData } from './core/app'
import Config from '@/core/config'
  import { TUILogin } from '@tencentcloud/tui-core';
  // #ifdef APP-PLUS || H5
  import { TUIChatKit } from './TUIKit';
  TUIChatKit.init();
  // #endif
  ﻿
  let vueVersion = 2;
  // #ifdef VUE3
  vueVersion = 3;
  // #endif
  ﻿
  // Required information
  // You can get userSig from TencentCloud chat console for Testing TUIKit.
  // Deploy production environment please get it from your server.
  // View https://cloud.tencent.com/document/product/269/32688
  uni.$SDKAppID = Config.get('SDKAppID'); // Your SDKAppID
  export default {

    /**
     * 全局变量
     */
    globalData: {

    },
	created() {
	},
	beforeUnmount() {
		this.prototype.$eventBus.off('call-method', this.onImLogin);
	},
    /**
     * 初始化完成时触发
     */
    onLaunch({ path, query, scene }) {
	  const app = this
      // 小程序主动更新
      this.updateManager()
      // app启动参数
      this.onStartupQuery(query)
	  // 登录im
	  this.onImLogin()
    },
	
    methods: {

      // app启动参数
      onStartupQuery(query) {
        // 获取二维码场景值
        const scene = getSceneData(query)
        // 记录推荐人ID
        const refereeId = query.refereeId ? query.refereeId : scene.uid
        refereeId > 0 && (this.saveRefereeId(refereeId))
      },
	  onImLogin(){
		  console.log(1)
		if(store.state.user.userId && store.state.user.sign){
			console.log(2)
			TUILogin.login({
			  	   SDKAppID: uni.$SDKAppID,
			  	   userID: store.state.user.userId.toString(),  
			  	   userSig: store.state.user.sign, 
			  	   useUploadPlugin: true, // If you need to send rich media messages, please set to true.
			  	   framework: `vue${vueVersion}` // framework used vue2 / vue3
			  	 }).catch(() => {});
		}
		
	  },
      // 记录推荐人ID
      saveRefereeId(refereeId) {
        store.dispatch('SaveRefereeId', refereeId)
      },

      // 小程序主动更新
      updateManager() {
        const updateManager = uni.getUpdateManager()
        updateManager.onCheckForUpdate(res => {
          // 请求完新版本信息的回调
          // console.log(res.hasUpdate)
        })
        updateManager.onUpdateReady(() => {
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，即将重启应用',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate()
              }
            }
          })
        })
        updateManager.onUpdateFailed(() => {
          // 新的版本下载失败
          uni.showModal({
            title: '更新提示',
            content: '新版本下载失败',
            showCancel: false
          })
        })
      }

    }

  }
</script>

<style lang="scss">
  /* 引入uView库样式 */
  @import "uview-ui/index.scss";
</style>

<style>
  /* 项目基础样式 */
  @import "./app.scss";

  .uni-app--showlayout+uni-tabbar.uni-tabbar-bottom,
  .uni-app--showlayout+uni-tabbar.uni-tabbar-bottom .uni-tabbar,
  .uni-app--showlayout+uni-tabbar.uni-tabbar-top,
  .uni-app--showlayout+uni-tabbar.uni-tabbar-top .uni-tabbar {
    left: var(--window-left);
    right: var(--window-right);
  }

</style>
